home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-09-14 | 830 b | 54 lines | [TEXT/MPS ] |
- /* This is test.g which tests simple AST refs and construction */
-
- <<
- typedef ANTLRCommonToken ANTLRToken;
- #include "DLGLexer.h"
-
- class AST : public ASTBase {
- public:
- ANTLRToken token;
- AST(ANTLRToken *t) { token = *t; }
- void preorder_action() {
- char *s = token.getText();
- printf(" %s", s);
- }
- };
-
- main()
- {
- ANTLRToken aToken;
- DLGFileInput in(stdin);
- DLGLexer scan(&in,2000);
- ANTLRTokenBuffer pipe(&scan);
- scan.setToken(&aToken);
- Expr parser(&pipe);
- parser.init();
-
- ASTBase *root = NULL;
- parser.e(&root);
- root->preorder();
- printf("\n");
- }
- >>
-
- #token "[\ \t\n]+" <<skip();>>
- #token Eof "@"
-
- class Expr { /* Define a grammar class */
-
- e : mult_expr ( ("\+"^|"\-"^) mult_expr )*
- ;
-
- mult_expr
- : atom ( ("\*"^|"\/"^) atom )*
- ;
-
- atom: IDENTIFIER
- | NUMBER
- ;
-
- }
-
- #token IDENTIFIER "[a-z]+"
- #token NUMBER "[0-9]+"
-